<?php
/**
 *
 */

class NconfigFace extends FaceBase {

	private $api = null;
	private $nohf = array();

	/**
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->api = n_api("nconfig");
		
		if (! in_array(N_ACTION, $this->nohf)) {
			n_mod('skin')->header();
		}
	}

	public function siteIndex() {
		$rs = $param = $this->param("scode:char,pevent:char");
		$mysql = n_api("mysql")->db001;
		$sql = "SELECT a.*,b.name AS tname FROM nconfig_data_site AS a
				LEFT JOIN nconfig_template_main AS b ON a.template_id = b.id
				WHERE scode = '{$param['scode']}' AND pevent = '{$param['pevent']}'";
		$rs['data'] = $mysql->select($sql);
		$this->_html("siteIndex", $rs);
	}

	public function siteNew() {
		$rs = $param = $this->param("scode:char:empty,pevent:char:empty");
		$mysql = n_api("mysql")->db001;
		$rs['data'] = $mysql->select("select * from nconfig_template_main where pevent='{$param['pevent']}'");
		$this->_html("siteNew", $rs);
	}
	
	public function siteEdit() {
		$rs = $param = $this->param('scode:char:ignore,id:uint:ignore,templateid:uint:ignore,pevent:char:empty');
		$mysql = n_api("mysql")->db001;

		if (isset($param['id'])) {
			// 已经存在的站点配置 通过查询获取模板信息
			$rs['info'] = $mysql->get("nconfig_data_site", $param['id']);
			$templateid = $rs['info']['template_id'];
			$isnew = FALSE;
		}
		else {
			// 新的站点配置 从参数获得模板信息
			$templateid = $param['templateid'];
			$isnew = TRUE;
		}
		$rs['templateid'] = $templateid;

		$rs['tinfo'] = $mysql->get("nconfig_template_main", $templateid);
		if (empty($rs['tinfo'])) {
			throw new PException("找不到模板");
		}

		if ($isnew && $rs['tinfo']['is_sington'] == 'Y') {
			$scode = $param['scode'];
			$pevent = $rs['tinfo']['pevent'];
			$ckey = $rs['tinfo']['ckey'];
			$config = $this->api->siteGet($scode, $pevent, $ckey);
			if (! empty($config)) {
				throw new PException("该配置不能重复设置");
			}
		}

		$rs['items'] = $mysql->select("select * from nconfig_template_items where main_id={$templateid}");
		if (empty($rs['items'])) {
			throw new PException("找不到数据项");
		}

		$this->_html('siteEdit', $rs);
	}

	public function siteSave(){
		$param = $this->param("id:char:ignore,scode:char:ignore,templateid:uint:ignore,title:char:empty");
		$mysql = n_api("mysql")->db001;

		if (isset($param['templateid'])) {
			// 新的站点配置 从参数获得模板信息
			$templateid = $param['templateid'];
			$isnew = TRUE;
		}
		else {
			// 旧的站点配置 通过查询获得模板信息
			$rs = $mysql->get("nconfig_data_site", $param['id']);
			$templateid = $rs['template_id'];
			$isnew = FALSE;
		}

		$tinfo = $mysql->get("nconfig_template_main", $templateid);
		// 检查模板是否存在
		if(empty($tinfo)){
			throw new PException("找不到模板");
		}

		// 检查是否要是但里模板且已经存在从该模板生成的规则
		if ($isnew && $tinfo['is_sington'] == 'Y') {
			$scode = $param['scode'];
			$pevent = $tinfo['pevent'];
			$ckey = $tinfo['ckey'];
			$config = $this->api->siteGet($scode, $pevent, $ckey);
			if (! empty($config)) {
				throw new PException("该配置不能重复设置");
			}
		}

		if(empty($param['id'])){
			$templateid = $param['templateid'];
			$id = $mysql->insert("nconfig_data_site",array("scode"=>$param['scode'],"template_id"=>$templateid,"title"=>$param['title'],"data"=>'{}'));
			$scode = $param['scode'];
			$pevent = $tinfo['pevent'];
			$isnew = TRUE;
		}else{
			$info = $mysql->get("nconfig_data_site",$param['id']);
			if(empty($info)){
				throw new PException("找不到数据");
			}
			$templateid = $info['template_id'];
			$id = $info['id'];
			$scode = $info['scode'];
			$pevent = $tinfo['pevent'];
			$isnew = FALSE;
		}
		$template = $mysql->select("select * from nconfig_template_items where main_id={$templateid}");
		foreach($template as $key=>$val){
			$item = null;
			switch($val['itype']){
				case 0:
					$item = $this->param("itemid{$val['id']}:char");
					break;
				case 1:
					$item = $this->param("itemid{$val['id']}:uint");
					break;
				case 2:
					$item = $this->param("itemid{$val['id']}:uint");
					break;
				case 10:
					$item = $this->param("itemid{$val['id']}:char");
					break;
				case 11:
					if(empty($_POST["itemid{$val['id']}"])){
						$item = array();
					}else{
						foreach($_POST["itemid{$val['id']}"] as $key=>$val){
							$item[] = $val;
						}
					}
					break;
			}
			$data[$val['ukey']] = $item["itemid{$val['id']}"];
		}

		$mysql->update("nconfig_data_site",array("title"=>$param['title'],"data"=>json_encode($data)),"id={$id}");

		$url = n_url('nconfig', 'siteIndex', array('scode' => $scode, 'pevent' => $pevent));
		n_mod("skin")->save("保存成功", 3, $url);
	}

	public function siteDel() {
		$param = $this->param('id:uint');

		$mysql = n_api("mysql")->db001;
		$sql = "SELECT a.*, b.pevent FROM nconfig_data_site AS a
				LEFT JOIN nconfig_template_main AS b ON a.template_id = b.id
				WHERE a.id = '{$param['id']}'";
		$info = $mysql->fetchFirst($sql);

		if (empty($info)) {
			throw new PException("找不到数据");
		}

		$mysql->rm("nconfig_data_site", $param['id']);
		$args = array('scode' => $info['scode'], 'pevent' => $info['pevent']);
		$url = n_url('nconfig', 'siteIndex', $args);
		n_mod("skin")->save("删除成功", 0, $url);
	}

	// 已废弃
	public function sync() {
		$rs['site'] = n_api("nsite")->getList();
		$this->_html("sync",$rs);
	}

	// 已废弃
	public function syncEdit() {
		$rs = $param = $this->param("src:char,dest:char");
		$rs['srcConf'] = $this->api->getSite($param['src']);
		$rs['destConf'] = $this->api->getSite($param['dest']);
		$this->_html("syncEdit", $rs);
	}

	// 已废弃
	public function syncSave() {
		$param = $this->param("scode:char");
		if (empty($param['scode'])) {
			throw new PException("参数异常");
		}

		$ids = implode(",", $_POST['ids']);
		if (! preg_match("/^\d{1,4}(,\d{1,4})*$/",$ids)) {
			throw new PException("参数异常");
		}

		$mysql = n_api("mysql")->db001;

		foreach ($_POST['ids'] as $key => $val) {
			$item = $this->api->getSiteById($val);
			unset($item['id']);
			$item['scode'] = $param['scode'];
			$mysql->save("nconfig_site",$item);
		}

		n_mod("skin")->save("保存成功");
	}

	public function __destruct() {
		if (! in_array(N_ACTION, $this->nohf)) {
			n_mod('skin')->footer();
		}
	}
}
?>
